package com.cskaoyan.service;

import com.cskaoyan.domain.MarketCollect;
import com.cskaoyan.domain.MarketCollectExample;
import com.cskaoyan.mapper.MarketCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService{
    @Autowired
    MarketCollectMapper collectMapper;
    @Override
    public int hasCollected(Integer id) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(id);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(example);
        MarketCollect marketCollect = collectMapper.selectByPrimaryKey(id);
        if(marketCollect != null){
        return marketCollect.getType();
        }else
            return 0;
    }

    @Override
    public List<MarketCollect> selectCollectionByUserId(int userId) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return collectMapper.selectByExample(example);
    }

    @Override
    public MarketCollect selectCollectionByValueId(int valueId) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        return collectMapper.selectOneByExample(example);
    }

    @Override
    @Transactional
    public void insertGoodIntoCollection(MarketCollect marketCollect) {
        collectMapper.insertSelective(marketCollect);
    }

    @Override
    public void deleteCollectionOFGood(Integer valueId) {
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        collectMapper.deleteByExample(example);
    }
}
